import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LC_102 {
        public List<List<Integer>> levelOrder(LC_144.TreeNode root) {
            List<List<Integer>> lists = new ArrayList<>();
            int size = 1;
            Queue<LC_144.TreeNode> que = new LinkedList<LC_144.TreeNode>();
            if(root == null){
                return lists;
            }
            que.offer(root);
            while(!que.isEmpty()){
                List<Integer> list = new ArrayList<>();
                int len = size;
                size = 0;
                while(len != 0){
                    len--;
                    LC_144.TreeNode node = que.poll();
                    list.add(node.val);
                    if(node.left != null){
                        que.offer(node.left);
                        size++;
                    }
                    if(node.right != null){
                        que.offer(node.right);
                        size++;
                    }
                }
                lists.add(list);
            }
            return lists;
        }

}
